/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import dao.CuaHangDAO2;
import dao.SanPhamDAO;
import dao.SanPhamKMDAO;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.ChuongTrinhKhuyenMai;
import pojo.CuaHang;
import pojo.SanPham;

/**
 *
 * @author ANNM
 */
public class QLKhuyenMai extends javax.swing.JInternalFrame {
    private DefaultComboBoxModel cbModel;
    private DefaultComboBoxModel cbModel1;
    private DefaultTableModel tableMode;

    /**
     * Creates new form QLKhuyenMai
     */
    public QLKhuyenMai() {
        initComponents();
        LayCuaHang();
        SanPhamKM();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jcbch = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jcbsanpham = new javax.swing.JComboBox();
        jTextField2 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jbThem = new javax.swing.JButton();
        jbXoa = new javax.swing.JButton();
        jbSua = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jdcNgaybatdau = new com.toedter.calendar.JDateChooser();
        jdcNgayketthuc = new com.toedter.calendar.JDateChooser();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Sản Phẩm Khuyến Mãi");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setText("Cửa Hàng");

        jcbch.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jcbch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbchActionPerformed(evt);
            }
        });

        jLabel5.setText("Sản Phẩm");

        jcbsanpham.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel6.setText("Khuyến Mãi");

        jbThem.setText("Thêm");
        jbThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbThemActionPerformed(evt);
            }
        });

        jbXoa.setText("Xoá");
        jbXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbXoaActionPerformed(evt);
            }
        });

        jbSua.setText("Sửa");
        jbSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSuaActionPerformed(evt);
            }
        });

        jButton4.setText("Thoát");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jLabel3.setText("Ngày Bắt Đầu");

        jLabel4.setText("Ngày Kết Thúc");

        jdcNgaybatdau.setDateFormatString("dd MM  yyyy");

        jdcNgayketthuc.setDateFormatString("dd MM yyyy");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(214, 214, 214)
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(143, 143, 143)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel6))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jdcNgaybatdau, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel3))
                                .addGap(47, 47, 47)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jdcNgayketthuc, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel4))
                                .addGap(18, 18, 18)
                                .addComponent(jbThem)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jbSua)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jbXoa))
                                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                        .addContainerGap(18, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jcbch, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel5)
                                .addComponent(jcbsanpham, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1)
                        .addGap(18, 18, 18))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jcbch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel6)
                                .addComponent(jLabel3)
                                .addComponent(jLabel5))
                            .addGap(11, 11, 11)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jcbsanpham, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(jLabel4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jdcNgaybatdau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jdcNgayketthuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(1, 1, 1)))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 5, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jbThem)
                            .addComponent(jbSua)
                            .addComponent(jbXoa))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton4)))
                .addGap(0, 26, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jcbchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbchActionPerformed
        // TODO add your handling code here:
        this.tableMode=new DefaultTableModel();
        this.tableMode.addColumn("Mã Khuyến Mãi");
        this.tableMode.addColumn("Mã Sản Phẩm");
        this.tableMode.addColumn("Tên Sản Phẩm ");
        this.tableMode.addColumn("Khuyến Mãi");
        this.tableMode.addColumn("Ngày Bắt Đầu");
        this.tableMode.addColumn("Ngày Kết Thúc");
        List<ChuongTrinhKhuyenMai> sp=null;
        CuaHang ch=(CuaHang)this.jcbch.getSelectedItem();

        int mach=ch.getMaCuaHang();
        LaySP(mach);
        sp=SanPhamKMDAO.LaySPKM(mach);
        //this.jComboBox1.seti
        for(int i=0;i<sp.size();i++){
            ChuongTrinhKhuyenMai sanPham=sp.get(i);
            Vector vt=new Vector();
            vt.add(sanPham.getMaChuongTrinh());
            vt.add(sanPham.getSanPham().getMaSanPham());
            vt.add(sanPham.getSanPham().getTenSanPham());
            vt.add(sanPham.getKhuyenMai());
            vt.add(sanPham.getNgayBatDau());
            vt.add(sanPham.getNgayKetThuc());
            this.tableMode.addRow(vt);

            //JOptionPane.showMessageDialog(this,sanPham.getTenSanPham() );
        }
        this.jTable1.setModel(tableMode);

    }//GEN-LAST:event_jcbchActionPerformed
    public void SanPhamKM(){
        this.tableMode=new DefaultTableModel();
        this.tableMode.addColumn("Mã Chương Khuyến Mãi ");
        this.tableMode.addColumn("Mã Sản Phẩm ");
        this.tableMode.addColumn("Tên Sản Phẩm ");
        this.tableMode.addColumn("Khuyến Mãi");
        this.tableMode.addColumn("Ngày Bắt Đầu");
        this.tableMode.addColumn("Ngày kết thúc");
        List<ChuongTrinhKhuyenMai> ds=SanPhamKMDAO.LayKM();
        for(int i=0;i<ds.size();i++){
            ChuongTrinhKhuyenMai km=ds.get(i);
            Vector vt=new Vector();
            
            vt.add(km.getMaChuongTrinh());
            vt.add(km.getSanPham().getMaSanPham());
//            JOptionPane.showMessageDialog(this, km.getSanPham().getMaSanPham());
            String tensp=km.getSanPham().getTenSanPham();
            vt.add(tensp);
            vt.add(km.getKhuyenMai());
            vt.add(km.getNgayBatDau());
            vt.add(km.getNgayKetThuc());
            this.tableMode.addRow(vt);
        }
        this.jTable1.setModel(tableMode);
    }
    public void LayCuaHang(){
        this.cbModel=new DefaultComboBoxModel();
        this.jcbch.setModel(cbModel);
         List<CuaHang> ds= new ArrayList<CuaHang>();
        ds=CuaHangDAO2.LayCuaHang();
        CuaHang temp=new CuaHang();
        temp.setMaCuaHang(65000);
        temp.setTenCuaHang("chon");
        this.cbModel.addElement(temp);
        for(int i=0;i<ds.size();i++){
            this.cbModel.addElement(ds.get(i));

        }
    }
    public void LaySP(int ch){
        this.cbModel1=new DefaultComboBoxModel();
        this.jcbsanpham.setModel(cbModel1);
         List<SanPham> ds= new ArrayList<SanPham>();
        //CuaHang mach=(CuaHang)this.jcbch.getSelectedItem();
         
        ds=SanPhamDAO.laysptheoch(ch);
        for(int i=0;i<ds.size();i++){
            //String tensp=ds.get(i);
            this.cbModel1.addElement(ds.get(i));

        }
    }
    private void jbThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbThemActionPerformed
        // TODO add your handling code here:
        SanPham sp=(SanPham)this.jcbsanpham.getSelectedItem();
        ChuongTrinhKhuyenMai km=SanPhamKMDAO.LayMa(sp.getMaSanPham());
        if(km !=null){
            JOptionPane.showMessageDialog(this, "Sản Phẩm Khuyến Mãi này đã tồn tại", "Cảnh Báo", JOptionPane.WARNING_MESSAGE);
        }else{
        if(this.jTextField2.getText().equals("")) {
        JOptionPane.showMessageDialog(this, "Bạn Chưa Điền Đầy Đủ Thông Tin", "Cảnh Báo", JOptionPane.ERROR_MESSAGE);
        }
        else{
            km=new ChuongTrinhKhuyenMai();
            
            km.setSanPham(sp);
            String khuyenmai=this.jTextField2.getText();
            km.setKhuyenMai(khuyenmai);
            Date ngay=this.jdcNgaybatdau.getDate();
            km.setNgayBatDau(ngay);
            ngay=this.jdcNgayketthuc.getDate();
            km.setNgayKetThuc(ngay);
            int kq=SanPhamKMDAO.ThemSPKM(km);
            if(kq==1){
                JOptionPane.showMessageDialog(this, "thanh cong");
                SanPhamKM();
            }else
            JOptionPane.showMessageDialog(this, "that bai");
        }
        //SanPhamKM();
        }
    }//GEN-LAST:event_jbThemActionPerformed

    private void jbXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbXoaActionPerformed
        // TODO add your handling code here:
        if(this.jTable1.getSelectedRow()<0){
            JOptionPane.showMessageDialog(this, "Bạn Chưa chọn khuyến mãi cần xoá", "Cảnh Báo", JOptionPane.ERROR_MESSAGE);

        }
        else{
            ChuongTrinhKhuyenMai km=new ChuongTrinhKhuyenMai();
            int dong=this.jTable1.getSelectedRow();
            String makm=this.jTable1.getValueAt(dong, 0).toString();
            Integer ma=Integer.valueOf(makm);
            km.setMaChuongTrinh(ma);
            String masp=this.jTable1.getValueAt(dong, 1).toString();
            Integer maSP=Integer.valueOf(masp);
            SanPham a=new SanPham();
            a.setMaSanPham(maSP);
            km.setSanPham(a);
           // km.setNgayBatDau((Date)this.jdcNgaybatdau.getDate());
            Date ngay=(Date)this.jTable1.getValueAt(dong, 4);
            km.setNgayBatDau(ngay);
            ngay=(Date)this.jTable1.getValueAt(dong, 5);
            km.setNgayKetThuc(ngay);
            //km.setTinhtrang(false);
            String khuyenmai=this.jTextField2.getText();
            km.setKhuyenMai(khuyenmai);
            int kq=SanPhamKMDAO.XoaSPKM(km);
            if(kq==1){
                JOptionPane.showMessageDialog(this, "thanh cong");
                SanPhamKM();
            }else
            JOptionPane.showMessageDialog(this, "that bai");
        }
    }//GEN-LAST:event_jbXoaActionPerformed

    private void jbSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSuaActionPerformed
        // TODO add your handling code here:
        if(this.jTable1.getSelectedRow()<0){
            JOptionPane.showMessageDialog(this, "Bạn Chưa chọn khuyến mãi cần Sửa", "Cảnh Báo", JOptionPane.ERROR_MESSAGE);

        }
        else{
            ChuongTrinhKhuyenMai km=new ChuongTrinhKhuyenMai();
            int dong=this.jTable1.getSelectedRow();
            String makm=this.jTable1.getValueAt(dong, 0).toString();
            Integer ma=Integer.valueOf(makm);
            km.setMaChuongTrinh(ma);
            String masp=this.jTable1.getValueAt(dong, 1).toString();
            String tensp=this.jTable1.getValueAt(dong, 2).toString();
            Integer maSP=Integer.valueOf(masp);
            SanPham a=new SanPham();
            a.setMaSanPham(maSP);
            a.setTenSanPham(tensp);
            km.setSanPham(a);
           // km.setTinhtrang(true);
            String khuyenmai=this.jTextField2.getText();
            km.setKhuyenMai(khuyenmai);
            int kq=SanPhamKMDAO.XoaSPKM(km);
            if(kq==1){
                JOptionPane.showMessageDialog(this, "thanh cong");
                SanPhamKM();
            }else
            JOptionPane.showMessageDialog(this, "that bai");
        }

    }//GEN-LAST:event_jbSuaActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        int dong=this.jTable1.getSelectedRow();
        String makm=this.jTable1.getValueAt(dong, 0).toString();
        String khuyenmai=this.jTable1.getValueAt(dong, 3).toString();
        //int masp=Integer.valueOf(ma);
        this.jTextField2.setText(khuyenmai);

    }//GEN-LAST:event_jTable1MouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JButton jbSua;
    private javax.swing.JButton jbThem;
    private javax.swing.JButton jbXoa;
    private javax.swing.JComboBox jcbch;
    private javax.swing.JComboBox jcbsanpham;
    private com.toedter.calendar.JDateChooser jdcNgaybatdau;
    private com.toedter.calendar.JDateChooser jdcNgayketthuc;
    // End of variables declaration//GEN-END:variables
}
